/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
}

/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */
var mergeTwoLists = function (list1, list2) {
    //空判断
    if (!list1) {
        return list2;
    }
    if (!list2) {
        return list1;
    }
    let newList = new ListNode(0);
    let curr = newList;
    while (list1 && list2) {
        //比较两个链表的值 放在新的链表中
        if (list1.val <= list2.val) {
            curr.next = list1;
            list1 = list1.next;
        } else {
            curr.next = list2;
            list2 = list2.next;
        }
        curr = curr.next;
    }
    //把不为空的链表挂载新的链表中
    curr.next = !list1 ? list2 : list1;
    return newList.next;
};
console.log(
    mergeTwoLists(
        new ListNode(
            1,
            new ListNode(
                2,
                new ListNode(
                    4
                )
            )
        ),
        new ListNode(
            1,
            new ListNode(
                3,
                new ListNode(
                    4
                )
            )
        )
    )
);

